<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 继承自己的类库
 * @author Cuil <lntschampion@163.com>
 * @date 2016-2-14
 */
class MY_Controller extends CI_Controller {

    private $come_url;

    function MY_Controller() {
        parent::__construct();

        $this->load->library('session');

        $ip = get_client_ip();
        $this->come_url = $ip . ' http://' . $_SERVER['SERVER_NAME'] . $_SERVER["REQUEST_URI"];
        $this->rlogs();
    }

    /**
     * 记录请求日志
     */
    private function rlogs() {
        $str = '';
        if ($_POST) {
            foreach ($_POST as $key => $val) {
                $str .=$key . "=" . $val;
            }
            $this->come_url .= '--post--' . $str;
        }
        Logs::info($this->come_url);
    }

    //记录返回数据
    private function returnData($data) {

        $str = '';

        if (!empty($data) && is_array($data)) {

            foreach ($data as $key => $val) {

                if (!empty($val) && is_array($val)) {

                    $str .= $this->returnData($val);
                } else {

                    @$str .= $key . "=" . $val;
                }
            }
        }

        return $str;
    }

    /**
     * 输出成功信息
     */
    public function printJson_data($data = array(), $info = '成功',$state = '2') {
        if (empty($data)) {
            $data = array();
        }
        $expression = array(
            'data' => $data,
            'code' => '0',
            'info' => $info
        );


        $str = $this->returnData($expression);
        Logs::data($this->come_url);
        
        $this->come_url = '--data--'.$str;
        Logs::data($this->come_url);

        if($state == 1){
            $expression = array(
                'data' => (object)($data),
                'code' => '0',
                'info' => $info
            );
        }
        printJson($expression);
    }

    /**
     * 输出错误信息
     */
    public function printJson($code, $info = '') {
        $expression = array(
            'code' => $code,
            'info' => $info
        );
        
        $str = $this->returnData($expression);
        Logs::data($this->come_url);
        
        $this->come_url = '--data--'.$str;
        Logs::data($this->come_url);
        
        printJson($expression);
    }
}
